<?php

declare(strict_types=1);

namespace app\api\model;

use app\api\model\Chapter as ModelChapter;
use app\api\model\Week as ModelWeek;
use app\api\model\Student as ModelStudent;
use app\api\model\Stage as ModelStage;
use app\api\model\Tool as ModelTool;
/**
 * 概述模型
 */
class Summarize extends Base
{

    /**
     *  # 获取课程概述
     * @param string $course_id
     * @return array
     */
    public static function getSummarize($course_id)
    {

        $statusCommentNum = 0;
        $data = Summarize::getStageWeekChapter($course_id);
        if ($data === false) {
            $courseTime = 0;
            $noPublicNum = 0;
        } else {
            $courseTime = count($data['week']);
            $noPublicNum = count($data['chapter']);
        }

        $student = ModelStudent::getStudent($course_id);
        if (empty($student->toArray())) {
            $peopleNum = 0;
        } else {
            $peopleNum = count($student->toArray());
        }

        return [
            'statusCommentNum' => $statusCommentNum,
            'courseTime' => $courseTime,
            'noPublicNum' => $noPublicNum,
            'peopleNum' => $peopleNum
        ];
    }


    /**
     * @param string $course_id
     * @return array|int
     */
    protected static function getStageWeekChapter($course_id)
    {
        $stage = ModelStage::getStage($course_id);
        if (empty($stage->toArray())) {
            return false;
        }
        $stage_ids = ModelTool::getKeyArray($stage->toArray(),'id');
        if ($stage_ids === false) {
            return false;
        }
        $week = ModelWeek::getWeek($stage_ids);
        if (empty($week->toArray())) {
            return false;
        }
        $week_ids = ModelTool::getKeyArray($week->toArray(),'id');
        $chapter = ModelChapter::getChapter($week_ids);
        if (empty($chapter->toArray())) {
            return false;
        } else {
            return [
                'stage' => $stage,
                'week' => $week,
                'chapter' => $chapter
            ];
        }
    }

}
